package com.doyens.sms.listener;

import com.aliyuncs.exceptions.ClientException;
import com.doyens.sms.config.SmsProperties;
import com.doyens.sms.utils.SmsUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Map;

@Component
public class SmsListener {

    @Autowired
    private SmsUtils smsUtils;

    @Autowired
    private SmsProperties smsProperties;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "doyens.sms.queue",durable = "true"),
            exchange = @Exchange(value = "doyens.sms.exchange",ignoreDeclarationExceptions = "true",type = ExchangeTypes.TOPIC),
            key = {"sms.verify.code"}))
    public void sendSms(Map<String,String> msg) throws ClientException {
           if (CollectionUtils.isEmpty(msg)){
               return;
           }
          String phone =  msg.get("phone");
          String code =  msg.get("code");
          if (StringUtils.isNotBlank(phone) && StringUtils.isNotBlank(code)){
              this.smsUtils.sendSms(phone,code,smsProperties.getSignName(),this.smsProperties.getVerifyCodeTemplate());
          }
    }


}
